package com.activiti.controller;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.activiti.model.User;
import com.activiti.model.UserGroup;
import com.activiti.model.VacTask;
import com.activiti.model.Vacation;
import com.activiti.service.IActivitiService;
import com.activiti.service.IVacationService;


@RestController
public class VacationController {

	@Autowired
	IVacationService vacationService;
	@Autowired
	IActivitiService activitiService;
	
	@GetMapping("init")
	public boolean initDb() {
		User user1 = new User("张三","123","部门职员");
		vacationService.addUser(user1);
		User user2= new User("李四","123","部门经理");
		vacationService.addUser(user2);
		User user3 = new User("王五","123","总经理");
		vacationService.addUser(user3);
		return true;
	}
	
	

	@PostMapping("/login")
	@ResponseBody
	public boolean login(HttpSession session, @RequestBody User user) {
		String userName = user.getUserName();
		String password = user.getPassword();
		boolean login = vacationService.login(userName, password);
		if (login) {
			session.setAttribute("userName", userName);
			return true;
		}
		return false;
	}

	@GetMapping("/exit")
	public String exit(HttpSession session) {
		session.removeAttribute("userName");
		return "login";
	}

	@GetMapping("/getAllUser")
	public Object getAllUser() {
		return vacationService.getAllUser();
	}

	@GetMapping("/getAllGroup")
	public Object getAllGroup() {
		return vacationService.getAllGroup();
	}

	@GetMapping("/getUserGroup")
	public Object getUserGroup(String groupId) {
		return vacationService.getUserGroup(groupId);
	}

	@PostMapping("/addUser")
	public Object addUser(@RequestBody User user) {
		return vacationService.addUser(user);
	}
	
	@PostMapping("/addUserToGroup")
	public Object addUserToGroup(@RequestBody User userGroup) {
		return vacationService.addUserToGroup(userGroup);
	}
	
	@PostMapping("/addGroup")
	public Object addGroup(@RequestBody UserGroup group) {
		return vacationService.addGroup(group);
	}

	@PostMapping("/startVac")
	public Object startVac(@RequestBody Vacation vac, HttpSession session) {
		String userName = (String) session.getAttribute("userName");
		return vacationService.startVac(userName, vac);
	}

	@GetMapping("/myVac")
	public Object myVac(HttpSession session) {
		String userName = (String) session.getAttribute("userName");
		return vacationService.myVac(userName);
	}

	@GetMapping("/myAudit")
	public Object myAudit(HttpSession session) {
		String userName = (String) session.getAttribute("userName");
		return vacationService.myAudit(userName);
	}

	@PostMapping("/passAudit")
	public Object passAudit(HttpSession session, @RequestBody VacTask vacTask) {
		String userName = (String) session.getAttribute("userName");
		return vacationService.passAudit(userName, vacTask);
	}

	@GetMapping("/myVacRecord")
	public Object myVacRecord(HttpSession session) {
		String userName = (String) session.getAttribute("userName");
		return vacationService.myVacRecord(userName);
	}

	@GetMapping("/myAuditRecord")
	public Object myAuditRecord(HttpSession session) {
		String userName = (String) session.getAttribute("userName");
		return vacationService.myAuditRecord(userName);
	}
}
